<template>
  <el-dialog
    :title="tableId ? '核实项目信息' : '新增项目'"
    width="800px"
    top="30px"
    :before-close="beforeClose"
    :visible.sync="visible"
    :close-on-click-modal="false"
  >
    <el-form
      :model="editCustomerData"
      v-loading="loading"
      label-position="left"
      size="small"
      :rules="!!tableId ? {} : rules"
      ref="form"
      label-width="130px"
    >
      <el-form-item>
        <h5 class="font-weight-bold" slot="label">主体信息</h5>
      </el-form-item>
      <el-form-item label="组织类型" prop="org_class">
        <select-farm-org
          v-model="editCustomerData.org_class"
          :disabled="!!tableId"
          class="form-control-ml"
          @select="handleOrgClassSelect"
        ></select-farm-org>
      </el-form-item>
      <el-form-item label="客户名称" prop="customer_name">
        <el-input
          class="form-control-ml"
          :disabled="!!tableId"
          v-model="editCustomerData.customer_name"
          placeholder="客户名称/个人姓名"
        ></el-input>
      </el-form-item>

      <el-form-item label="联系电话" prop="mobile_no">
        <el-input
          :disabled="!!tableId"
          v-model="editCustomerData.mobile_no"
          class="form-control-ml"
          placeholder="请输入联系电话"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="营业执照号"
        v-if="isCommon"
        :rules="{ required: true, message: '请输入营业执照号', trigger: 'blur' }"
        prop="register_no"
      >
        <el-input
          v-model="editCustomerData.register_no"
          :disabled="!!tableId"
          class="form-control-ml"
          placeholder="营业执照号"
        >
          <el-button slot="append" class="input-group-text" type="info" @click="linkto">
            工商信息
          </el-button>
        </el-input>
      </el-form-item>

      <el-form-item label="负责人" prop="corp_name" v-if="isCommon">
        <el-input
          :disabled="!!tableId"
          v-model="editCustomerData.corp_name"
          class="form-control-ml"
          placeholder="请输入负责人名称"
        ></el-input>
      </el-form-item>
      <el-form-item :label="isCommon ? '负责人身份证' : '证件号码'" prop="lender_idcard">
        <el-input
          :disabled="!!tableId"
          v-model="editCustomerData.lender_idcard"
          class="form-control-ml"
          placeholder="请输入身份证号"
        ></el-input>
      </el-form-item>
      <el-form-item label="经营年限" prop="bussiness_years">
        <el-input
          :disabled="!!tableId"
          v-model="editCustomerData.bussiness_years"
          class="form-control-ml"
          placeholder="请输入经营年限"
        >
          <span slot="append">年</span>
        </el-input>
      </el-form-item>
      <el-form-item label="经营地址" prop="address_code">
        <select-address
          :disabled="!!tableId"
          class="form-control-ml"
          clearable
          v-model="editCustomerData.address_code"
          auto-top
        ></select-address>
      </el-form-item>
      <el-form-item label="详细地址">
        <el-input
          :disabled="!!tableId"
          v-model="editCustomerData.address_detail"
          class="form-control-ml"
          placeholder="详细地址"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <h5 class="font-weight-bold" slot="label">金融信息</h5>
      </el-form-item>
      <el-form-item label="资产总额" prop="assets_funds">
        <el-input
          :disabled="!!tableId"
          v-model="editCustomerData.assets_funds"
          class="form-control-ml"
          placeholder="请输入资产总额"
        >
          <span slot="append">万元</span>
        </el-input>
      </el-form-item>
      <el-form-item label="资产情况" prop="assets_des">
        <el-input
          :disabled="!!tableId"
          v-model="editCustomerData.assets_des"
          class="form-control-ml"
          placeholder="资产情况"
          type="textarea"
        ></el-input>
      </el-form-item>
      <el-form-item label="负债情况" prop="debt_des">
        <el-input
          :disabled="!!tableId"
          v-model="editCustomerData.debt_des"
          class="form-control-ml"
          placeholder="负债情况"
          type="textarea"
        ></el-input>
      </el-form-item>
      <el-form-item label="负债总额" prop="debt_funds">
        <el-input
          :disabled="!!tableId"
          v-model="editCustomerData.debt_funds"
          class="form-control-ml"
          placeholder="请输入负债总额"
        >
          <span slot="append">万元</span>
        </el-input>
      </el-form-item>
      <el-form-item label="是否涉诉">
        <el-radio v-model="editCustomerData.is_litigation" label="0" :disabled="!!tableId">
          否
        </el-radio>
        <el-radio v-model="editCustomerData.is_litigation" label="1" :disabled="!!tableId">
          是
        </el-radio>
      </el-form-item>
      <el-form-item
        label="涉诉信息"
        prop="litigation_des"
        v-if="+editCustomerData.is_litigation === 1"
      >
        <el-input
          :disabled="!!tableId"
          v-model="editCustomerData.litigation_des"
          class="form-control-ml"
          placeholder="涉诉信息"
          type="textarea"
        ></el-input>
      </el-form-item>
      <el-form-item label="征信情况" prop="credit_des">
        <el-input
          :disabled="!!tableId"
          v-model="editCustomerData.credit_des"
          class="form-control-ml"
          placeholder="请输入征信情况"
          type="textarea"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <h5 class="font-weight-bold" slot="label">贷款信息</h5>
      </el-form-item>
      <el-form-item label="贷款产品" prop="project_type_id">
        <select-product
          :disabled="!!tableId"
          class="form-control-ml"
          @select="getProductName"
          v-model="editCustomerData.project_type_id"
          :ind-code="indCode"
          placeholder="选择贷款产品"
        ></select-product>
      </el-form-item>
      <el-form-item label="申请金额" prop="apply_funds">
        <el-input
          :disabled="!!tableId"
          v-model="editCustomerData.apply_funds"
          class="form-control-ml"
          placeholder="请输入申请金额"
        >
          <span slot="append">万元</span>
        </el-input>
      </el-form-item>

      <el-form-item label="贷款年限" prop="load_years">
        <el-input
          :disabled="!!tableId"
          v-model="editCustomerData.load_years"
          class="form-control-ml"
          placeholder="请输入贷款年限"
        >
          <span slot="append">年</span>
        </el-input>
      </el-form-item>
      <el-form-item label="贷款用途" prop="funds_use_dir">
        <el-input
          :disabled="!!tableId"
          v-model="editCustomerData.funds_use_dir"
          class="form-control-ml"
          placeholder="请输入贷款用途"
        ></el-input>
      </el-form-item>
      <el-form-item label="贷款意向银行" prop="bank_id">
        <select-bank
          :disabled="!!tableId"
          :product-id="editCustomerData.project_type_id"
          v-model="editCustomerData.bank_id"
          clearable
          placeholder="贷款意向银行"
          class="form-control-ml"
        ></select-bank>
      </el-form-item>
    </el-form>
    <el-form
      :model="infoData"
      v-loading="loading"
      label-position="left"
      size="small"
      :rules="!!tableId ? {} : infoRules"
      ref="info"
      label-width="130px"
    >
      <el-form-item>
        <h5 class="font-weight-bold" slot="label">经营信息</h5>
        <span class="text-sm">提供证明材料更容易完成放款</span>
      </el-form-item>
      <el-form-item label="经营类型" prop="business_type">
        <c-select
          :disabled="!!tableId"
          v-model="infoData.business_type"
          class="form-control-ml"
          placeholder="经营类型"
          :options="enumBusinessType"
        ></c-select>
      </el-form-item>
      <template v-if="+infoData.business_type === 1">
        <el-form-item label="近12个月产量" prop="output" key="output-1">
          <el-input
            :disabled="!!tableId"
            v-model="infoData.output"
            class="form-control-ml"
            placeholder="请输入近12个月产量"
          >
            <span slot="append">吨</span>
          </el-input>
        </el-form-item>
        <el-form-item label="近12个月销售收入" prop="sell_income" key="sell_income-1">
          <el-input
            :disabled="!!tableId"
            v-model="infoData.sell_income"
            class="form-control-ml"
            placeholder="请输入近12个月销售收入"
          >
            <span slot="append">万元</span>
          </el-input>
        </el-form-item>
        <el-form-item label="证明材料（银行/微信/支付宝流水、购销单据/合同）" key="attachUpload">
          <template v-if="!!tableId">
            <attach-item
              class="form-control-ml"
              v-for="(p, index) of attachList"
              :attach-data="p"
              :key="index"
            ></attach-item>
          </template>
          <upload-oss
            v-else
            multiple
            :dir="dir"
            :file-name="0"
            class="form-control-ml"
            ref="attachUpload"
            :file-list="attachList"
            :on-complete="v => uploadComplete(v, 'income_attachment')"
          ></upload-oss>
        </el-form-item>
        <el-form-item label="是否购买农业保险" prop="is_buy_insurance" key="is_buy_insurance-1">
          <c-radio-group
            :disabled="!!tableId"
            :options="commonRadioOptions"
            v-model="infoData.is_buy_insurance"
          ></c-radio-group>
        </el-form-item>
        <el-form-item label="证明材料（保单）" key="insuranceUpload">
          <template v-if="!!tableId">
            <attach-item
              class="form-control-ml"
              v-for="(p, index) of attachList"
              :attach-data="p"
              :key="index"
            ></attach-item>
          </template>
          <upload-oss
            v-else
            multiple
            :dir="dir"
            :file-name="0"
            class="form-control-ml"
            ref="insuranceUpload"
            :file-list="insuranceList"
            :on-complete="v => uploadComplete(v, 'ins_attachment')"
          ></upload-oss>
        </el-form-item>
        <el-form-item label="种植面积" prop="plant_area" key="plant_area-1">
          <el-input
            v-model="infoData.plant_area"
            class="form-control-ml"
            placeholder="请输入种植面积"
          >
            <span slot="append">亩</span>
          </el-input>
        </el-form-item>
        <el-form-item label="证明材料（土地流转合同、保险单）" key="plantUpload">
          <upload-oss
            multiple
            :dir="dir"
            :file-name="0"
            class="form-control-ml"
            ref="plantUpload"
            :file-list="plantList"
            :on-complete="v => uploadComplete(v, 'plant_attachment')"
          ></upload-oss>
        </el-form-item>

        <el-form-item label="种植品种" prop="plant_category" key="plant_category-1">
          <el-input
            v-model="infoData.plant_category"
            class="form-control-ml"
            placeholder="请输入种植品种"
          ></el-input>
        </el-form-item>
      </template>
      <template v-if="+infoData.business_type === 2">
        <el-form-item label="厂房面积" prop="workshop_area" key="workshop_area-2">
          <el-input
            :disabled="!!tableId"
            v-model="infoData.workshop_area"
            class="form-control-ml"
            placeholder="请输入厂房面积"
          >
            <span slot="append">㎡</span>
          </el-input>
        </el-form-item>
        <el-form-item label="证明材料（租赁合同或产权证明）" key="workUpload">
          <template v-if="!!tableId">
            <attach-item
              class="form-control-ml"
              v-for="(p, index) of workList"
              :attach-data="p"
              :key="index"
            ></attach-item>
          </template>
          <upload-oss
            v-else
            multiple
            :dir="dir"
            :file-name="0"
            class="form-control-ml"
            ref="workUpload"
            :file-list="workList"
            :on-complete="v => uploadComplete(v, 'workshop_attachment')"
          ></upload-oss>
        </el-form-item>
        <el-form-item label="从业人数" prop="emp_total" key="emp_total-2">
          <el-input
            :disabled="!!tableId"
            v-model="infoData.emp_total"
            class="form-control-ml"
            placeholder="请输入从业人数"
          >
            <span slot="append">人</span>
          </el-input>
        </el-form-item>
        <el-form-item label="证明材料（工资单、工资支付凭证）" key="empUpload">
          <template v-if="!!tableId">
            <attach-item
              class="form-control-ml"
              v-for="(p, index) of empList"
              :attach-data="p"
              :key="index"
            ></attach-item>
          </template>
          <upload-oss
            v-else
            multiple
            :dir="dir"
            :file-name="0"
            class="form-control-ml"
            ref="empUpload"
            :file-list="empList"
            :on-complete="v => uploadComplete(v, 'emp_attachment')"
          ></upload-oss>
        </el-form-item>
        <el-form-item label="近12个月产量" prop="output" key="output-1">
          <el-input
            :disabled="!!tableId"
            v-model="infoData.output"
            class="form-control-ml"
            placeholder="请输入近12个月产量"
          >
            <span slot="append">吨</span>
          </el-input>
        </el-form-item>
        <el-form-item label="近12个月销售收入" prop="sell_income" key="sell_income-1">
          <el-input
            :disabled="!!tableId"
            v-model="infoData.sell_income"
            class="form-control-ml"
            placeholder="请输入近12个月销售收入"
          >
            <span slot="append">万元</span>
          </el-input>
        </el-form-item>
        <el-form-item label="证明材料（银行/微信/支付宝流水、购销单据/合同）" key="attachUpload">
          <template v-if="!!tableId">
            <attach-item
              class="form-control-ml"
              v-for="(p, index) of attachList"
              :attach-data="p"
              :key="index"
            ></attach-item>
          </template>
          <upload-oss
            v-else
            multiple
            :dir="dir"
            :file-name="0"
            class="form-control-ml"
            ref="attachUpload"
            :file-list="attachList"
            :on-complete="v => uploadComplete(v, 'income_attachment')"
          ></upload-oss>
        </el-form-item>

        <el-form-item label="是否符合国家环保政策" prop="is_fit_env" key="is_fit_env-2">
          <c-radio-group
            :disabled="!!tableId"
            :options="commonRadioOptions"
            v-model="infoData.is_fit_env"
          ></c-radio-group>
        </el-form-item>

        <el-form-item
          label="证明材料（环评报告或环评表）"
          key="envUpload"
          v-if="+infoData.is_fit_env === 1"
        >
          <template v-if="!!tableId">
            <attach-item
              class="form-control-ml"
              v-for="(p, index) of envList"
              :attach-data="p"
              :key="index"
            ></attach-item>
          </template>
          <upload-oss
            v-else
            multiple
            :dir="dir"
            :file-name="0"
            class="form-control-ml"
            ref="envUpload"
            :file-list="envList"
            :on-complete="v => uploadComplete(v, 'env_attachment')"
          ></upload-oss>
        </el-form-item>
        <el-form-item label="生产品种" prop="produce_category" key="produce_category-2">
          <el-input
            v-model="infoData.produce_category"
            class="form-control-ml"
            placeholder="请输入生产品种"
          ></el-input>
        </el-form-item>
        <el-form-item label="证明材料（成品照片）" key="productUpload">
          <upload-oss
            multiple
            :dir="dir"
            :file-name="0"
            ref="productUpload"
            class="form-control-ml"
            :file-list="productList"
            :on-complete="v => uploadComplete(v, 'pro_category_attachment')"
          ></upload-oss>
        </el-form-item>
        <el-form-item label="近12个月税收情况" prop="tax_des" key="tax_des-2">
          <el-input
            :disabled="!!tableId"
            v-model="infoData.tax_des"
            class="form-control-ml"
            placeholder="请输入近12个月税收情况"
          ></el-input>
        </el-form-item>
        <el-form-item label="证明材料">
          <template v-if="!!tableId">
            <attach-item
              class="form-control-ml"
              v-for="(p, index) of taxDesAttachment"
              :attach-data="p"
              :key="index"
            ></attach-item>
          </template>
          <upload-oss
            v-else
            multiple
            :dir="dir"
            :file-name="0"
            class="form-control-ml"
            ref="taxDesUpload"
            :file-list="taxDesAttachment"
            :on-complete="v => uploadComplete(v, 'tax_des_attachment')"
          ></upload-oss>
        </el-form-item>
      </template>
      <template v-if="+infoData.business_type === 3">
        <el-form-item label="近12个月收入" prop="sell_income" key="sell_income-3">
          <el-input
            :disabled="!!tableId"
            v-model="infoData.sell_income"
            class="form-control-ml"
            placeholder="请输入近12个月收入"
          >
            <span slot="append">万元</span>
          </el-input>
        </el-form-item>
        <el-form-item label="证明材料（银行/微信/支付宝流水、购销单据/合同）" key="attachUpload">
          <template v-if="!!tableId">
            <attach-item
              class="form-control-ml"
              v-for="(p, index) of attachList"
              :attach-data="p"
              :key="index"
            ></attach-item>
          </template>
          <upload-oss
            v-else
            multiple
            :dir="dir"
            :file-name="0"
            class="form-control-ml"
            ref="attachUpload"
            :file-list="attachList"
            :on-complete="v => uploadComplete(v, 'income_attachment')"
          ></upload-oss>
        </el-form-item>
        <el-form-item label="是否有烘干线" prop="is_dry_line" key="is_dry_line-3">
          <c-radio-group
            :options="commonRadioOptions"
            v-model="infoData.is_dry_line"
          ></c-radio-group>
        </el-form-item>
        <el-form-item
          label="烘干线数量"
          prop="dry_count"
          v-if="+infoData.is_dry_line === 1"
          key="dry_count-3"
        >
          <el-input
            v-model="infoData.dry_count"
            class="form-control-ml"
            placeholder="请输入烘干线数量"
          ></el-input>
        </el-form-item>

        <el-form-item
          label="证明材料（购买发票、购买合同、现场设备照片）"
          key="isDryLine"
          v-if="+infoData.is_dry_line === 1"
        >
          <upload-oss
            multiple
            :dir="dir"
            :file-name="0"
            class="form-control-ml"
            ref="isDryLine"
            :file-list="isDryLineList"
            :on-complete="v => uploadComplete(v, 'dry_attachment')"
          ></upload-oss>
        </el-form-item>
        <el-form-item label="是否有固定摊位" prop="is_fix_booth" key="is_fix_booth-3">
          <c-radio-group
            :options="commonRadioOptions"
            v-model="infoData.is_fix_booth"
          ></c-radio-group>
        </el-form-item>

        <el-form-item
          label="摊位面积"
          prop="booth_area"
          v-if="+infoData.is_fix_booth === 1"
          key="booth_area-3"
        >
          <el-input
            v-model="infoData.booth_area"
            class="form-control-ml"
            placeholder="请输入摊位面积"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="自有/租赁"
          prop="booth_type"
          v-if="+infoData.is_fix_booth === 1"
          key="booth_type-3"
        >
          <c-radio-group :options="enmuBoothType" v-model="infoData.booth_type"></c-radio-group>
        </el-form-item>

        <el-form-item
          key="isFixBooth"
          label="证明材料（租赁或购买合同、现场照片）"
          v-if="+infoData.is_fix_booth === 1"
        >
          <upload-oss
            multiple
            :dir="dir"
            :file-name="0"
            class="form-control-ml"
            ref="isFixBooth"
            :file-list="isFixBoothList"
            :on-complete="v => uploadComplete(v, 'booth_attachment')"
          ></upload-oss>
        </el-form-item>
      </template>

      <template v-if="+infoData.business_type === 4">
        <el-form-item
          label="近12个月产量"
          prop="output"
          key="output-4"
          :rules="{ required: true, message: '请输入近12个月产量', trigger: 'blur' }"
        >
          <el-input
            :disabled="!!tableId"
            v-model="infoData.output"
            class="form-control-ml"
            placeholder="请输入近12个月产量"
          ></el-input>
        </el-form-item>
        <el-form-item label="近12个月销售收入" prop="sell_income" key="sell_income-4">
          <el-input
            :disabled="!!tableId"
            v-model="infoData.sell_income"
            class="form-control-ml"
            placeholder="请输入近12个月销售收入"
          >
            <span slot="append">万元</span>
          </el-input>
        </el-form-item>
        <el-form-item label="证明材料（银行/微信/支付宝流水、购销单据/合同）" key="attachUpload">
          <template v-if="!!tableId">
            <attach-item
              class="form-control-ml"
              v-for="(p, index) of attachList"
              :attach-data="p"
              :key="index"
            ></attach-item>
          </template>
          <upload-oss
            v-else
            multiple
            :dir="dir"
            :file-name="0"
            class="form-control-ml"
            ref="attachUpload"
            :file-list="attachList"
            :on-complete="v => uploadComplete(v, 'income_attachment')"
          ></upload-oss>
        </el-form-item>
        <el-form-item label="是否购买农业保险" prop="is_buy_insurance" key="is_buy_insurance-4">
          <c-radio-group
            :disabled="!!tableId"
            :options="commonRadioOptions"
            v-model="infoData.is_buy_insurance"
          ></c-radio-group>
        </el-form-item>
        <el-form-item label="证明材料（保单）" key="insuranceUpload">
          <template v-if="!!tableId">
            <attach-item
              class="form-control-ml"
              v-for="(p, index) of attachList"
              :attach-data="p"
              :key="index"
            ></attach-item>
          </template>
          <upload-oss
            v-else
            multiple
            :dir="dir"
            :file-name="0"
            class="form-control-ml"
            ref="insuranceUpload"
            :file-list="insuranceList"
            :on-complete="v => uploadComplete(v, 'ins_attachment')"
          ></upload-oss>
        </el-form-item>
        <el-form-item
          label="养殖规模"
          prop="plant_area"
          key="plant_area-4"
          :rules="{ required: true, message: '请输入养殖规模', trigger: 'blur' }"
        >
          <el-input
            v-model="infoData.plant_area"
            class="form-control-ml"
            placeholder="请输入养殖规模"
          ></el-input>
        </el-form-item>
        <el-form-item label="证明材料（土地流转合同、保险单、其他）" key="plantUpload">
          <upload-oss
            multiple
            :dir="dir"
            :file-name="0"
            class="form-control-ml"
            ref="plantUpload"
            :file-list="plantList"
            :on-complete="v => uploadComplete(v, 'plant_attachment')"
          ></upload-oss>
        </el-form-item>

        <el-form-item label="养殖品种" prop="plant_category" key="plant_category-4">
          <el-input
            v-model="infoData.plant_category"
            class="form-control-ml"
            placeholder="请输入养殖品种"
          ></el-input>
        </el-form-item>
      </template>

      <el-form-item v-if="!!tableId">
        <span class="text-warning">请核实以上可改信息，若有误请修改后提交，若无误可直接提交</span>
      </el-form-item>
      <el-form-item v-if="!tableId">
        <el-button type="primary" class="btn-long" @click="submit" :loading="saveLoading">
          保存
        </el-button>
        <el-button class="btn-long" @click="beforeClose" :loading="saveLoading">取消</el-button>
      </el-form-item>
      <el-form-item v-else>
        <el-button type="primary" class="btn-long" @click="submit" :loading="saveLoading">
          保存
        </el-button>
        <el-button type="danger" class="btn-long" @click="handleReject" :loading="saveLoading">
          拒绝
        </el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import SelectProduct from '@/gov/views/bus/import/class/components/SelectProduct'
import SelectFarmOrg from '@/components/select/SelectFarmOrg'
import SelectAddress from '@/components/select/SelectAddress'
import { idCardValidator, mobileValidator, registerNoValidator } from '@/common/plugins/validator'
import SelectBank from '@/gov/views/bus/import/class/components/SelectBank'
import { arrayToString, arrayFromString } from '@vimi/utils-tool'
import { mapGetters } from 'vuex'
import { attachAdapter } from '@/bank/components/attach/index'
import AttachItem from '@/bank/components/attach/Item'
export const enumBusinessType = {
  1: '种植类',
  4: '养殖类',
  2: '加工类',
  3: '商贸流通类',
}
export const enmuBoothType = [
  { label: '租赁', value: '1' },
  { label: '自有', value: '2' },
]
export default {
  components: {
    SelectProduct,
    SelectFarmOrg,
    SelectAddress,
    SelectBank,
    AttachItem,
  },
  props: {
    indCode: String,
    visible: Boolean,
    tableId: String,
    formData: { type: Object, default: () => ({}) },
  },
  data() {
    const isNumberValidator = (rule, value, callback) => {
      if (!value) {
        callback()
      } else if (!Number.isFinite(+value)) {
        callback(new Error('请输入正确的数值'))
      } else {
        callback()
      }
    }
    return {
      commonRadioOptions: [
        { label: '否', value: '0' },
        { label: '是', value: '1' },
      ],
      enmuBoothType,
      enumBusinessType,
      loading: false,
      infoRules: {
        workshop_area: [
          { required: true, message: '请输入厂房面积', trigger: 'blur' },
          { validator: isNumberValidator, trigger: 'blur' },
        ],
        emp_total: [
          { required: true, message: '请输入从业人数', trigger: 'blur' },
          { validator: isNumberValidator, trigger: 'blur' },
        ],
        business_type: { required: true, message: '请选择经营类型', trigger: 'change' },
        plant_area: [
          { required: true, message: '请输入种植面积', trigger: 'blur' },
          { validator: isNumberValidator, trigger: 'blur' },
        ],
        plant_category: { required: true, message: '请输入品种', trigger: 'blur' },
        produce_category: { required: true, message: '请输入生产品种', trigger: 'blur' },
        output: [
          { required: true, message: '请输入近12个月产量', trigger: 'blur' },
          { validator: isNumberValidator, trigger: 'blur' },
        ],

        sell_income: [
          { required: true, message: '请输入近12个月收入', trigger: 'blur' },
          {
            validator: isNumberValidator,
            trigger: 'blur',
          },
        ],
        dry_count: { required: true, message: '请输入烘干线数量', trigger: 'blur' },
        booth_area: { required: true, message: '请输入摊位面积', trigger: 'blur' },
        booth_type: { required: true, message: '请选择摊位类型', trigger: 'change' },
      },
      infoData: {},
      editCustomerData: {},
      rules: {
        project_type_id: { required: true, message: '请选择贷款产品', trigger: 'change' },
        org_class: { required: true, message: '请选择组织类型', trigger: 'change' },
        customer_name: { required: true, message: '请输入客户名称', trigger: 'blur' },
        corp_name: { required: true, message: '请输入负责人名字', trigger: 'blur' },
        address_code: { required: true, message: '请选择经营地址', trigger: 'change' },
        bussiness_years: { validator: isNumberValidator, trigger: 'blur' },
        lender_idcard: [
          { required: true, message: '请输入身份证号', trigger: 'blur' },
          { validator: idCardValidator, message: '身份证号格式错误', trigger: 'blur' },
        ],
        register_no: {
          validator: registerNoValidator,
          message: '营业执照号格式错误',
          trigger: 'blur',
        },
        mobile_no: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
          { validator: mobileValidator, message: '手机号码错误', trigger: 'blur' },
        ],
        assets_funds: [
          // { required: true, message: '请输入资产总额', trigger: 'blur' },
          { validator: isNumberValidator, trigger: 'blur' },
        ],
        apply_funds: [
          { required: true, message: '请输入申请金额', trigger: 'blur' },
          { validator: isNumberValidator, trigger: 'blur' },
        ],
        load_years: [
          { required: true, message: '请输入贷款年限', trigger: 'blur' },
          { validator: isNumberValidator, trigger: 'blur' },
        ],
        funds_use_dir: { required: true, message: '请输入贷款用途', trigger: 'blur' },
      },
      isCommon: false,
      attachList: [], // 近12个月收入证明材料
      isDryLineList: [],
      isFixBoothList: [],
      plantList: [],
      insuranceList: [],
      workList: [], // 加工类 厂房附件
      productList: [], // 加工类 成品附件
      empList: [], // 加工类 从业人数附件
      envList: [], // 加工类 环保附件
      letterList: [], // 推荐函
      taxDesAttachment: [], // 税收证明
      tableListName: 'credit_industrial_pepper',
      tableName: 'credit_industrial',
      saveLoading: false,
      uploadTotal: 0,
      uploadCount: 0,
    }
  },
  computed: {
    ...mapGetters(['provinceMainBusinessEnum', 'user']),
    dir() {
      return `gov/${this.indCode}/${this.editCustomerData.mobile_no}`
    },
  },
  watch: {
    visible: {
      handler: function () {
        if (this.visible) {
          this.getData()
        }
      },
      immediate: true,
    },
  },
  created() {
    this.$store.dispatch('getProvinceMainBusiness')
  },
  mounted() {},
  methods: {
    handleReject() {
      this.$prompt('请输入拒绝说明', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValidator: value => (!value ? '请输入拒绝说明' : true),
      }).then(({ value }) => {
        this.$api
          .updateData(this.$serviceName, this.tableName, this.tableId, {
            recommend_status: 2,
            confuse_person: this.user.realName,
            confuse_des: value,
            confuse_time: this.$utils.dateFormat(new Date()),
          })
          .then(() => {
            this.$message.success('已拒绝！')
            this.$emit('success', true)
            this.beforeClose()
          })
          .catch(() => {
            this.$message.error('操作失败，请重试！')
          })
      })
    },
    init() {
      this.infoData = {
        is_buy_insurance: '0',
        is_dry_line: '0',
        is_fix_booth: '0',
        is_fit_env: '0',
        // business_type: '1',
      }
      this.editCustomerData = {
        is_litigation: '0',
      }
      this.letterList = []
      this.attachList = []
      this.isDryLineList = []
      this.isFixBoothList = []
      this.plantList = []
      this.insuranceList = []
      this.workList = []
      this.productList = []
      this.empList = []
      this.envList = []
      this.taxDesAttachment = []
      this.$nextTick(() => {
        this.$refs.form && this.$refs.form.clearValidate()
        this.$refs.info && this.$refs.info.clearValidate()
      })
    },
    submit() {
      this.$refs.form.validate(valid => {
        this.$refs.info.validate(info => {
          if (valid && info) {
            this.submitAttach()
          } else {
            this.$message.error('输入错误，请重新输入！')
          }
        })
      })
    },
    getProductName(name) {
      this.editCustomerData.project_type_name = name
    },
    submitData() {
      this.editCustomerData.corp_name =
        this.editCustomerData.corp_name || this.editCustomerData.customer_name
      this.editCustomerData.main_business_code = this.indCode
      this.editCustomerData.main_business = this.provinceMainBusinessEnum[this.indCode]
      this.editCustomerData.customer_type = this.isCommon ? 1 : 2
      const formData = {
        masterTableInfo: JSON.stringify(this.editCustomerData),
        masterTableName: this.tableName,
        slaveTableInfo: JSON.stringify([{ ...this.infoData }]),
        slaveTableName: this.tableListName,
      }
      if (this.tableId) {
        this.putData(formData)
      } else {
        this.postData(formData)
      }
    },
    putData(data) {
      this.saveLoading = true
      this.$api
        .relatedUpdate(this.$serviceName, data)
        .then(() => {
          this.$message.success('修改成功！')
          this.getData()
          this.init()
          this.beforeClose()
          this.$emit('success', true)
        })
        .catch(err => {
          if (err) {
            const errStr = typeof err.data === 'string' ? err.data : '操作失败，请重试！'
            return this.$message.error(`${errStr}`)
          }
        })
        .finally(() => {
          this.saveLoading = false
        })
    },
    postData(data) {
      this.saveLoading = true
      this.$api
        .relatedBatch(this.$serviceName, data)
        .then(() => {
          this.$message.success('添加项目成功！')
          this.init()
          this.beforeClose()
          this.$emit('success', true)
        })
        .catch(err => {
          if (err) {
            const errStr = typeof err.data === 'string' ? err.data : '添加项目失败，请重试！'
            return this.$message.error(`${errStr}`)
          }
        })
        .finally(() => {
          this.saveLoading = false
        })
    },
    submitAttach() {
      this.uploadTotal = 0
      this.uploadCount = 0
      if (this.$refs.attachUpload) {
        this.uploadTotal++
      }
      if (this.$refs.insuranceUpload) {
        this.uploadTotal++
      }
      if (this.$refs.plantUpload) {
        this.uploadTotal++
      }
      if (this.$refs.workUpload) {
        this.uploadTotal++
      }
      if (this.$refs.empUpload) {
        this.uploadTotal++
      }
      if (this.$refs.envUpload) {
        this.uploadTotal++
      }
      if (this.$refs.productUpload) {
        this.uploadTotal++
      }
      if (this.$refs.taxDesUpload) {
        this.uploadTotal++
      }
      if (this.$refs.isDryLine) {
        this.uploadTotal++
      }
      if (this.$refs.isFixBooth) {
        this.uploadTotal++
      }
      if (this.$refs.attachUpload) {
        this.$refs.attachUpload.submit()
      }
      // 种植类
      if (this.$refs.insuranceUpload) {
        this.$refs.insuranceUpload.submit()
      }
      if (this.$refs.plantUpload) {
        this.$refs.plantUpload.submit()
      }

      // 加工类
      if (this.$refs.workUpload) {
        this.$refs.workUpload.submit()
      }
      if (this.$refs.empUpload) {
        this.$refs.empUpload.submit()
      }
      if (this.$refs.envUpload) {
        this.$refs.envUpload.submit()
      }
      if (this.$refs.productUpload) {
        this.$refs.productUpload.submit()
      }
      if (this.$refs.taxDesUpload) {
        this.$refs.taxDesUpload.submit()
      }

      // 商贸流通
      if (this.$refs.isDryLine) {
        this.$refs.isDryLine.submit()
      }
      if (this.$refs.isFixBooth) {
        this.$refs.isFixBooth.submit()
      }

      if (+this.uploadTotal < 1) {
        this.submitData()
      }
    },
    uploadComplete(files, key) {
      this.infoData[key] = arrayToString(files.map(it => it.url))
      this.uploadCount++
      if (this.uploadCount >= this.uploadTotal) {
        this.submitData()
      }
    },
    linkto() {
      window.open('http://www.gsxt.gov.cn/index.html')
    },
    handleOrgClassSelect(val, item = {}) {
      this.editCustomerData.org_class_name = item.orgClassName || ''
      this.isCommon = +item.customerType === 1
    },
    getData() {
      if (!this.tableId) {
        this.init()
        return
      }
      this.loading = true
      this.$api
        .getData(this.$serviceName, this.tableName, {
          'credit_industrial_id.eq': this.tableId,
          subKey: 'credit_industrial_pepper.credit_industrial_id',
        })
        .then(res => {
          const data = res?.data?.[0] ?? {}
          const files = [
            'address_code',
            'address_detail',
            'apply_funds',
            'org_class',
            'lender_idcard',
            'customer_name',
            'assets_funds',
            'mobile_no',
            'register_no',
            'load_years',
            'assets_des',
            'debt_des',
            'debt_funds',
            'is_litigation',
            'litigation_des',
            'project_type_id',
            'funds_use_dir',
            'bank_id',
            'credit_industrial_id',
            'bussiness_years',
          ]
          files.forEach(key => {
            this.editCustomerData[key] = data[key]
          })
          this.editCustomerData = { ...this.editCustomerData }
          this.letterList = this.getAttach(data.recommend_letter_attachment)
          this.isCommon = +data.customer_type === 1
          const infoData = data?.credit_industrial_pepper?.[0] ?? {}
          const infoFiles = [
            'booth_area',
            'booth_type',
            'business_type',
            'id',
            'credit_industrial_id',
            'dry_count',
            'emp_total',
            'is_buy_insurance',
            'is_dry_line',
            'is_fit_env',
            'is_fix_booth',
            'output',
            'plant_area',
            'plant_category',
            'produce_category',
            'sell_income',
            'workshop_area',
            'tax_des',
            'tax_des_attachment',
          ]
          infoFiles.forEach(key => {
            this.infoData[key] = infoData[key]
          })
          this.attachList = this.getAttach(infoData.income_attachment) // 近12个月收入证明材料

          this.isDryLineList = this.getAttach(infoData.dry_attachment)
          this.isFixBoothList = this.getAttach(infoData.booth_attachment)
          this.plantList = this.getAttach(infoData.plant_attachment)
          this.insuranceList = this.getAttach(infoData.ins_attachment)
          this.workList = this.getAttach(infoData.workshop_attachment)
          this.productList = this.getAttach(infoData.pro_category_attachment)
          this.empList = this.getAttach(infoData.emp_attachment)
          this.envList = this.getAttach(infoData.env_attachment)
          this.taxDesAttachment = this.getAttach(infoData.tax_des_attachment)
          this.infoData = { ...this.infoData }
        })
        .finally(() => {
          this.$nextTick(() => {
            this.$refs.form.clearValidate()
            this.$refs.info.clearValidate()
          })
          this.loading = false
        })
    },
    getAttach(data) {
      return arrayFromString(data)
        .map(img => {
          return { url: img, name: img.replace(/.*\/(.*)$/, '$1'), attachment: img }
        })
        .map(item => {
          return { ...item, ...attachAdapter(item) }
        })
    },
    beforeClose() {
      this.$emit('update:visible', false)
    },
    handleSuccess() {
      this.$emit('success', true)
    },
  },
}
</script>

<style lang="scss" scoped></style>
